package com.zjjxy.service.impl;

import com.zjjxy.constant.MessageConstant;
import com.zjjxy.exception.PasswordErrorException;
import com.zjjxy.mapper.StudentMapper;
import com.zjjxy.pojo.dto.StudentLoginDTO;
import com.zjjxy.pojo.entity.Question;
import com.zjjxy.pojo.entity.Student;
import com.zjjxy.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

@Service
public class StudentServiceImpl implements StudentService {

    @Autowired
    private StudentMapper studentMapper;

    /**
     * 学生登录
     * @param studentLoginDTO
     * @return
     */
    public Student login(StudentLoginDTO studentLoginDTO) {
        String no = studentLoginDTO.getNo();
        String password = studentLoginDTO.getPassword();

        //根据学生学号查询学生信息
        Student student = studentMapper.getByNo(no);

        //密码比对，对前端传过来的明文密码进行md5加密处理
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        if (!password.equals(student.getPassword())) {
            //密码错误
            throw new PasswordErrorException(MessageConstant.PASSWORD_ERROR);
        }

        return student;
    }

    /**
     * 查询题目
     * @return
     */
    public Question selectQuestion() {
        Question question = studentMapper.selectByUpdateTimeAndStatus();
        return question;
    }
}
